using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.IO.IsolatedStorage;
using System.Text;
namespace MicroMsg.Plugin.DriftBottle.Scene
{
	public class NetSceneOpenBottle : NetSceneBaseEx<OpenBottleRequest, OpenBottleResponse, OpenBottleRequest.Builder>
	{
		private const string TAG = "NetSceneOpenBottle";
		private int mBottleType = -1;
		private string mBottleInfo = "";
		private string strToTalker = "";
		private int mLocalStartPos;
		private int mTotalLen;
		private int mDoCount;
		private byte[] receiveBytes;
		private ContentInfo mBigContent;
		public bool doScene(string bottleInfo, int bottleType)
		{
			return bottleInfo != null && (bottleType == 1 || bottleType == 3) && new NetSceneOpenBottle
			{
				mBottleType = bottleType,
				mBottleInfo = bottleInfo,
				strToTalker = bottleInfo
			}.doSceneEx();
		}
		private bool doSceneEx()
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.BottleInfo = this.mBottleInfo;
			this.mBuilder.MsgType = this.mBottleType;
			ContentInfo.Builder builder = ContentInfo.CreateBuilder();
			builder.StartPos = 0;
			builder.TotLen = 0;
			ContentInfo smallContentInfo = builder.Build();
			if (this.mBottleType != 1)
			{
				if (this.mBottleType != 3)
				{
					return false;
				}
			}
			ContentInfo.Builder builder2 = ContentInfo.CreateBuilder();
			builder2.StartPos = this.mLocalStartPos;
			builder2.TotLen = this.mTotalLen;
			this.mBigContent = builder2.Build();
			this.mBuilder.SetBigContentInfo(this.mBigContent);
			this.mBuilder.SetSmallContentInfo(smallContentInfo);
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/openbottle";
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
			this.mDoCount++;
			return true;
		}
		protected override void onFailed(OpenBottleRequest request, OpenBottleResponse response)
		{
			Log.smethod_1("NetSceneOpenBottle", "Get my friend failed system error!");
			if (NetScenePickBottle.isPickCanceled())
			{
				DriftBottleServiceCenter.DelBottle(this.mBottleInfo);
				return;
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new BottleRetbase(BottleEvent.ON_NETSCENE_OPENBOTTLE_FAIL), null);
		}
		protected override void onSuccess(OpenBottleRequest request, OpenBottleResponse response)
		{
			if (NetScenePickBottle.isPickCanceled())
			{
				Log.smethod_5("NetSceneOpenBottle", "Pick bootle has been canceled");
				DriftBottleServiceCenter.DelBottle(this.mBottleInfo);
				return;
			}
			if (response == null)
			{
				Log.smethod_1("NetSceneOpenBottle", "response == null");
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_FAIL, null), null);
				return;
			}
			if (response.BaseResponse.Ret != 0)
			{
				Log.smethod_1("NetSceneOpenBottle", "OpenBottleRequest bottle ret is not MMOK, Ret = " + ((RetConst)response.BaseResponse.Ret).ToString());
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_FAIL, null), null);
				return;
			}
			if (this.mBottleType == 1)
			{
				string hashString = MD5Core.GetHashString(this.strToTalker + Util.getNowMilliseconds());
				ChatMsg chatMsg = new ChatMsg();
				chatMsg.strTalker = this.strToTalker;
				byte[] array = response.BigContentBuff.Content.Buffer.ToByteArray();
				int num = 0;
				int num2 = array.Length - 1;
				while (num2 >= 0 && array[num2] < 32)
				{
					num++;
					num2--;
				}
				int num3 = array.Length - num;
				byte[] array2 = new byte[num3];
				Buffer.BlockCopy(array, 0, array2, 0, num3);
				chatMsg.strMsg = Util.ByteArrayToString(array2);
				if (chatMsg.strMsg.EndsWith("\0"))
				{
					Log.smethod_5("NetSceneOpenBottle", "msgInfo.strContent.EndsWith 0 ");
					chatMsg.strMsg = chatMsg.strMsg.Remove(chatMsg.strMsg.get_Length() - 1, 1);
				}
				chatMsg.nIsSender = 0;
				chatMsg.nMsgType = NetSceneOpenBottle.bottleType2ChatMsgType(this.mBottleType);
				chatMsg.nCreateTime = (long)Util.getNowSeconds();
				chatMsg.nStatus = 2;
				chatMsg.strClientMsgId = hashString;
				chatMsg.nMsgSvrID = (int)Util.getNowMilliseconds() / 1000;
				StorageMgr.chatMsg.addMsg(chatMsg);
				this.insertBottle(chatMsg, 0);
				Log.smethod_4("NetSceneOpenBottle", "Receive a text Bottle Msg success!");
				EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_SUCCESS, chatMsg), null);
				MsgNotifyMgr.DoNotify(chatMsg.strTalker);
				return;
			}
			if (this.mBottleType == 3)
			{
				if (response.BigContentBuff == null)
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_FAIL, null), null);
					Log.smethod_1("NetSceneOpenBottle", "response.BigContentBuf is null");
					return;
				}
				if (response.BigContentBuff.TotLen < response.BigContentBuff.StartPos + response.BigContentBuff.Content.Buffer.Length || response.BigContentBuff.TotLen <= 0)
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_FAIL, null), null);
					Log.smethod_1("NetSceneOpenBottle", "response has error param");
					return;
				}
				if (this.mLocalStartPos != response.BigContentBuff.StartPos)
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_FAIL, null), null);
					Log.smethod_1("NetSceneOpenBottle", "startPos has error");
					return;
				}
				Log.smethod_5("NetSceneOpenBottle", "Buf length = " + response.BigContentBuff.Content.Buffer.Length);
				if (this.receiveBytes == null)
				{
					this.receiveBytes = new byte[response.BigContentBuff.TotLen];
				}
				Buffer.BlockCopy(response.BigContentBuff.Content.Buffer.ToByteArray(), 0, this.receiveBytes, response.BigContentBuff.StartPos, response.BigContentBuff.Content.Buffer.Length);
				this.mTotalLen = response.BigContentBuff.TotLen;
				this.mLocalStartPos += response.BigContentBuff.Content.Buffer.Length;
				if (this.mLocalStartPos == response.BigContentBuff.TotLen)
				{
					string hashString2 = MD5Core.GetHashString(this.strToTalker + Util.getNowMilliseconds());
					int hashCode = MD5Core.GetHashString(this.strToTalker + Util.getNowMilliseconds()).GetHashCode();
					ChatMsg chatMsg2 = new ChatMsg();
					chatMsg2.nMsgSvrID = hashCode;
					chatMsg2.strTalker = this.strToTalker;
					chatMsg2.strMsg = NetSceneOpenBottle.getBottleContent(this.mTotalLen, (int)response.VoiceLength, this.strToTalker);
					chatMsg2.nIsSender = 0;
					chatMsg2.nMsgType = NetSceneOpenBottle.bottleType2ChatMsgType(this.mBottleType);
					chatMsg2.nCreateTime = (long)Util.getNowSeconds();
					chatMsg2.strClientMsgId = hashString2;
					chatMsg2.nMsgSvrID = MD5Core.GetHashString(this.strToTalker + Util.getNowMilliseconds()).GetHashCode();
					chatMsg2.nStatus = 2;
					string text = MsgVoiceStorage.buildVoiceFilePath(chatMsg2);
					if (!NetSceneOpenBottle.writeVoiceToFile(text, this.receiveBytes))
					{
						EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_FAIL, null), null);
						Log.smethod_1("NetSceneOpenBottle", "write vioce file error");
						return;
					}
					chatMsg2.strPath = text;
					StorageMgr.chatMsg.addMsg(chatMsg2);
					this.insertBottle(chatMsg2, (int)response.VoiceLength);
					Log.smethod_1("NetSceneOpenBottle", "recieve a Voice bottle msg success! time = " + response.VoiceLength);
					EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_SUCCESS, chatMsg2), null);
					MsgNotifyMgr.DoNotify(chatMsg2.strTalker);
					return;
				}
				else
				{
					if (this.mLocalStartPos > response.BigContentBuff.TotLen || this.mDoCount > 10)
					{
						EventCenter.postEvent(EventConst.ON_NETSCENE_GRIFTBOTTLE_EVENT, new OpenBottleRet(BottleEvent.ON_NETSCENE_OPENBOTTLE_FAIL, null), null);
						Log.smethod_1("NetSceneOpenBottle", "recieve Length is large than totLen, error");
						return;
					}
					this.doSceneEx();
				}
			}
		}
		private void insertBottle(ChatMsg chatMsg_0, int voiceLen = 0)
		{
			if (chatMsg_0 == null)
			{
				return;
			}
			if (chatMsg_0.strTalker == null)
			{
				return;
			}
			string[] array = chatMsg_0.strTalker.Split(new char[]
			{
				':'
			});
			if (array != null && array.Length >= 2)
			{
				MicroMsg.Storage.BottleInfo bottleInfo = new MicroMsg.Storage.BottleInfo();
				bottleInfo.nChildCount = 0;
				bottleInfo.nIsSender = 0;
				bottleInfo.nMsgLocalID = chatMsg_0.nMsgLocalID;
				bottleInfo.nCreateTime = (long)Util.getNowSeconds();
				bottleInfo.nMsgType = chatMsg_0.nMsgType;
				bottleInfo.strBottleID = array[1];
				bottleInfo.strContent = chatMsg_0.strMsg;
				bottleInfo.strVoicelen = string.Concat(voiceLen);
				bottleInfo.strUserName = array[0];
				bottleInfo.strParentClientid = MD5Core.GetHashString(chatMsg_0.strTalker);
				StorageMgr.bottle.vmethod_0(bottleInfo);
				return;
			}
		}
		public static bool writeVoiceToFile(string filePath, byte[] bytes)
		{
			bool result;
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (userStoreForApplication.FileExists(filePath))
					{
						userStoreForApplication.DeleteFile(filePath);
					}
					using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream(filePath, 4, userStoreForApplication))
					{
						isolatedStorageFileStream.Seek(0L, 0);
						UTF8Encoding uTF8Encoding = new UTF8Encoding();
						byte[] bytes2 = uTF8Encoding.GetBytes("#!AMR\n");
						byte[] array = new byte[bytes2.Length + bytes.Length];
						Buffer.BlockCopy(bytes2, 0, array, 0, bytes2.Length);
						Buffer.BlockCopy(bytes, 0, array, bytes2.Length, bytes.Length);
						isolatedStorageFileStream.Write(array, 0, array.Length);
						isolatedStorageFileStream.Flush();
						result = true;
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_5("storage", "writeToFile file fail" + ex);
				result = false;
			}
			return result;
		}
		public static int bottleType2ChatMsgType(int bottleType)
		{
			switch (bottleType)
			{
			case 1:
				return 1;
			case 2:
				return 3;
			case 3:
				return 34;
			case 4:
				return 43;
			default:
				return -1;
			}
		}
		public static string getBottleContent(int totalLen, int voiceTimeLen, string fromusername)
		{
			return string.Concat(new object[]
			{
				"<msg><voicemsg  endflag=\"",
				1,
				"\" length=\"",
				totalLen,
				"\" voicelength=\"",
				voiceTimeLen,
				"\" clientmsgid=\"0\" fromusername=\"",
				fromusername,
				"\" downcount=\"",
				0,
				"\" /></msg>"
			});
		}
	}
}
